import 'package:flutter/material.dart';
import 'package:my_flutter_stomp_core/my_flutter_stomp_core.dart';

class WebSocketDemo extends StatefulWidget {
  const WebSocketDemo({super.key});

  @override
  State<WebSocketDemo> createState() => _WebSocketDemoState();
}

class _WebSocketDemoState extends State<WebSocketDemo> {
  String _currentState = StompClientService.stateDisconnected;

  UnsubscribeFn? _unsubscribeFn;

  late StompSubscriber _stompSubscriber;

  @override
  void initState() {
    super.initState();

    StompClientService.init(
      endpoint: 'ws://192.168.1.6:8080/hello-websocket/stomp',
      connectHeaders: {
        'Authorization': 'Bearer token-value-goes-here',
      },
      onChangeState: (state) {
        if (mounted) {
          setState(() => _currentState = state);
        }
      },
    );

    _stompSubscriber = StompClientService.spawn();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              _currentState,
              style: Theme.of(context).textTheme.titleMedium,
              textAlign: TextAlign.center,
            ),
            FilledButton.tonal(
              onPressed: () {
                StompClientService.connect();
              },
              child: const Text('Connect'),
            ),
            FilledButton.tonal(
              onPressed: () {
                StompClientService.disconnect();
              },
              child: const Text('Disconnect'),
            ),
            FilledButton.tonal(
              onPressed: () {
                _unsubscribeFn = _stompSubscriber.subscribe(
                  destination: '/topic/greetings',
                  callback: (res) {
                    // print(res);
                  },
                  headers: {
                    'Authorization': 'Bearer token-value-goes-here',
                  },
                );
              },
              child: const Text('Subscribe'),
            ),
            FilledButton.tonal(
              onPressed: () {
                _stompSubscriber.unsubscribe(_unsubscribeFn!, null);
              },
              child: const Text('Unsubscribe'),
            ),
            FilledButton.tonal(
              onPressed: () async {
                // On server side destination = StompEndpointRegistry.addEndpoint + @MessageMapping in backend service.
                // final res = await _stompSubscriber.sendAndGet(
                //   destination: '/ws/hello',
                //   headers: {
                //     'Authorization': 'Bearer token-value-goes-here',
                //   },
                //   webSocketBody: WebSocketBody(
                //     payload: {
                //       'id': 1,
                //       'name': 'web',
                //     },
                //   ),
                // );

                // print(res);
              },
              child: const Text('Send'),
            ),
          ],
        ),
      ),
    );
  }
}
